import javax.swing.*;

import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class ICSGui2 {
        private static JTextField textEvent;
        private static JTextField textLocation;
        private static JTextField textResource;
        private static final ButtonGroup buttonGroup = new ButtonGroup();
        
        static String event;
        static String location;
        static String comments;
        static String rClass;
        static String resource;

        /**
         * @param args
         * GUI for the calendar project
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
               
                final JFrame frame = new JFrame();
                frame.setSize(450,740);
                frame.setTitle("ICS Calendar Project");
               
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(null);
               
                JPanel panel = new JPanel();
                panel.setBounds(0, 0, 440, 704);
                frame.getContentPane().add(panel);
                panel.setLayout(null);
               
                JLabel lblCreateCalendarFile = new JLabel("Create Calendar File");
                lblCreateCalendarFile.setBounds(137, 5, 125, 16);
                panel.add(lblCreateCalendarFile);
               
                //Event
                JLabel lblName = new JLabel("Event:");
                lblName.setBounds(23, 47, 61, 16);
                panel.add(lblName);
                textEvent = new JTextField();
                textEvent.setBounds(96, 41, 298, 28);
                panel.add(textEvent);
                textEvent.setColumns(10);
               
                //Radio buttons for public and private
                final JRadioButton radioPublic = new JRadioButton("Public");
                buttonGroup.add(radioPublic);
                radioPublic.setBounds(106, 70, 141, 23);
                panel.add(radioPublic);
                final JRadioButton radioPrivate = new JRadioButton("Private");
                buttonGroup.add(radioPrivate);
                radioPrivate.setBounds(232, 70, 141, 23);
                panel.add(radioPrivate);
               
                //Location
                JLabel lblLocation = new JLabel("Location:");
                lblLocation.setBounds(23, 115, 61, 16);
                panel.add(lblLocation);
                textLocation = new JTextField();
                textLocation.setBounds(96, 109, 298, 28);
                panel.add(textLocation);
                textLocation.setColumns(10);
               
                //Resources
                JLabel lblResources = new JLabel("Resources:");
                lblResources.setBounds(23, 170, 74, 16);
                panel.add(lblResources);
                textResource = new JTextField();
                textResource.setBounds(96, 164, 298, 28);
                panel.add(textResource);
                textResource.setColumns(10);
               
                //Recurring Events
                JLabel lblRecurring = new JLabel("Recurring:");
                lblRecurring.setBounds(23, 234, 74, 16);
                panel.add(lblRecurring);
                final JComboBox comboBox_12 = new JComboBox();
                comboBox_12.setModel(new DefaultComboBoxModel(new String[] {"None", "Daily", "Monthly", "Yearly"}));
                comboBox_12.setBounds(96, 230, 298, 27);
                panel.add(comboBox_12);
                
                //Start date/time
                JLabel lblStart = new JLabel("Start:");
                lblStart.setBounds(23, 295, 61, 16);
                panel.add(lblStart);
                JLabel lblDatetime = new JLabel("Date/Time");
                lblDatetime.setBounds(23, 313, 74, 16);
                panel.add(lblDatetime);
                //Date/Time start mo
                final JComboBox comboBox = new JComboBox();
                comboBox.setModel(new DefaultComboBoxModel(new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
                comboBox.setBounds(96, 291, 131, 27);
                panel.add(comboBox);            
                //Date/Time start day
                final JComboBox comboBox_1 = new JComboBox();
                comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
                comboBox_1.setBounds(232, 291, 61, 30);
                panel.add(comboBox_1);
                //Date/Time start yr
                final JComboBox comboBox_2 = new JComboBox();
                comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026"}));
                comboBox_2.setBounds(305, 291, 89, 27);
                panel.add(comboBox_2);
              //Date/Time start hr
                final JComboBox comboBox_3 = new JComboBox();
                comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22","23"}));
                comboBox_3.setBounds(153, 320, 74, 27);
                panel.add(comboBox_3);
                //Date/Time start min
                final JComboBox comboBox_4 = new JComboBox();
                comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"}));
                comboBox_4.setBounds(232, 320, 74, 27);
                panel.add(comboBox_4);
               
                //End date/time
                JLabel lblEnd = new JLabel("End:");
                lblEnd.setBounds(23, 381, 61, 16);
                panel.add(lblEnd);              
                JLabel lblDatetime_1 = new JLabel("Date/Time");
                lblDatetime_1.setBounds(23, 399, 74, 16);
                panel.add(lblDatetime_1);
                //Date/Time end mo
                final JComboBox comboBox_5 = new JComboBox();
                comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
                comboBox_5.setBounds(96, 388, 131, 27);
                panel.add(comboBox_5);
                //Date/Time end day
                final JComboBox comboBox_6 = new JComboBox();
                comboBox_6.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
                comboBox_6.setBounds(232, 388, 61, 27);
                panel.add(comboBox_6);
                //Date/Time end yr
                final JComboBox comboBox_7 = new JComboBox();
                comboBox_7.setModel(new DefaultComboBoxModel(new String[] {"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026"}));
                comboBox_7.setBounds(305, 388, 89, 27);
                panel.add(comboBox_7);
                //Date/Time end hr
                final JComboBox comboBox_8 = new JComboBox();
                comboBox_8.setModel(new DefaultComboBoxModel(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12","13","14","15","16","17","18","19","20","21","22","23"}));
                comboBox_8.setBounds(153, 417, 74, 27);
                panel.add(comboBox_8);
                //Date/Time end min
                final JComboBox comboBox_9 = new JComboBox();
                comboBox_9.setModel(new DefaultComboBoxModel(new String[] {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"}));
                comboBox_9.setBounds(232, 417, 74, 27);
                panel.add(comboBox_9);
               
                //Description
                JLabel lblDescription = new JLabel("Comments:");
                lblDescription.setBounds(24, 511, 84, 16);
                panel.add(lblDescription);
                final JTextArea textComment = new JTextArea();
                textComment.setBounds(107, 507, 276, 143);
                panel.add(textComment);
                
                //Time Zone options
                JLabel lblTimeZone = new JLabel("Time Zone:");
                lblTimeZone.setBounds(23, 464, 80, 14);
                panel.add(lblTimeZone);
                final JComboBox comboBox_10 = new JComboBox();
                comboBox_10.setModel(new DefaultComboBoxModel(new String[] {"US/Hawaii", "Pacific/Samoa" , "US/Alaska", "US/Pacific", "US/Arizona", 
                		"US/Central", "US/Eastern", "Chile/Continental", "America/Buenos_Aires", "Brazil/DeNoronha", "Europe/London", "Europe/Paris", "Africa/Cairo", 
                		"Asia/Baghdad", "Asia/Dubai", "Asia/Karachi", "Asia/Bishkek", "Asia/Ho_Chi_Minh", "Australia/Perth", "Asia/Ho_Chi_Minh", "Asia/Tokyo", "Australia/Sydney", 
                		"Pacific/Pohnpei", "Pacific/Fiji"}));
                comboBox_10.setBounds(106, 457, 150, 28);
                panel.add(comboBox_10);
               
                //Generate file button
                JButton btnGenerateFile = new JButton("Generate File");
                btnGenerateFile.addActionListener(new ActionListener() {
                       
                	
	public void actionPerformed(ActionEvent e) {
        event = textEvent.getText();
        location = textLocation.getText();
        comments = textComment.getText();
        if (radioPublic.isSelected()) {
            rClass = "PUBLIC";
        }
        if (radioPrivate.isSelected()) {
            rClass = "PRIVATE";
        }
        resource = textResource.getText();
                        
        String startMonth = (String) comboBox.getSelectedItem();
        String startDay = (String) comboBox_1.getSelectedItem();
        String startYear = (String) comboBox_2.getSelectedItem();
        String startHour = (String) comboBox_3.getSelectedItem();
        String startMin = (String) comboBox_4.getSelectedItem();
        String timeZone = (String) comboBox_10.getSelectedItem();
        String ocurrence = (String) comboBox_12.getSelectedItem();
                                        
        String endMonth = (String) comboBox_5.getSelectedItem();
        String endDay = (String) comboBox_6.getSelectedItem();
        String endYear = (String) comboBox_7.getSelectedItem();
        String endHour = (String) comboBox_8.getSelectedItem();
        String endMin = (String) comboBox_9.getSelectedItem();
        // String endTOD = (String) comboBox_11.getSelectedItem();
                                        
        // String finalStartDateTime = errorCheck.formatDateTime(startYear, startMonth, startDay, startHour, startMin, startTOD);
        //String finalEndDateTime = errorCheck.formatDateTime(endYear, endMonth, endDay, endHour, endMin, endTOD);
        //mushes the time into one variable
            String startTime = startHour + "" + startMin;
            String endTime = endHour + "" + endMin;
        //converts the strings in to Ints
            int stYear = Integer.parseInt(startYear);
            int stMonth = ErrorCheck2.changeMonth(startMonth);
            int stDay = Integer.parseInt(startDay);
            int stTime =  Integer.parseInt(startTime);
            int edYear = Integer.parseInt(endYear);                                       
            int edMonth = ErrorCheck2.changeMonth(endMonth);
            int edDay = Integer.parseInt(endDay);
            int edTime =  Integer.parseInt(endTime);
            //error checks
            ErrorCheck2.errorCheck(stMonth, stDay, stYear, stTime);
            stDay = ErrorCheck2.theDay;
            ErrorCheck2.errorCheck(edMonth, edDay, edYear, edTime);
            edDay = ErrorCheck2.theDay;
            //error check to see if event makes sense
            if(stYear > edYear){
            	JOptionPane.showMessageDialog(null, "Your start year (" + stYear + ")  must be less than or equal to your end year (" + edYear + "). Changed the start year to " + edYear);
            	stYear = edYear;  
            	if(stMonth > edMonth){
            		JOptionPane.showMessageDialog(null, "Your start month (" + stMonth + ")  must be less than or equal to your end month (" + edMonth + "). Changed the start month to " + edMonth);
                	stMonth = edMonth;
                	if(stDay > edDay){
                		JOptionPane.showMessageDialog(null, "Your start day (" + stDay + ")  must be less than or equal to your end day (" + edDay + "). Changed the start day to " + edDay);
                      	stDay = edDay;
                      	if(stTime > edTime){
                      		JOptionPane.showMessageDialog(null, "Your start time (" + startHour+":"+startMin + ")  must be less than or equal to your end time (" + endHour+":"+endMin + "). Changed the start time to " +endHour+":"+endMin);
                        	stTime = edTime;
                      	}
                	}
            	}
            	
            }
            else if(stYear == edYear && stMonth > edMonth){
            		JOptionPane.showMessageDialog(null, "Your start month (" + stMonth + ")  must be less than or equal to your end month (" + edMonth + "). Changed the start month to " + edMonth);
                	stMonth = edMonth;
                	if(stDay > edDay){
                		JOptionPane.showMessageDialog(null, "Your start day (" + stDay + ")  must be less than or equal to your end day (" + edDay + "). Changed the start day to " + edDay);
                      	stDay = edDay;
                      	if(stTime > edTime){
                      		JOptionPane.showMessageDialog(null, "Your start time (" + startHour+":"+startMin + ")  must be less than or equal to your end time (" + endHour+":"+endMin + "). Changed the start time to " +endHour+":"+endMin);
                        	stTime = edTime;
                      	}
                	}
          	}
            else if(stYear == edYear && stMonth == edMonth && stDay > edDay){
         		JOptionPane.showMessageDialog(null, "Your start day (" + stDay + ")  must be less than or equal to your end day (" + edDay + "). Changed the start day to " + edDay);
              	stDay = edDay; 
              	if(stTime > edTime){
              		JOptionPane.showMessageDialog(null, "Your start time (" + startHour+":"+startMin + ")  must be less than or equal to your end time (" + endHour+":"+endMin + "). Changed the start time to " +endHour+":"+endMin);
                	stTime = edTime;
              	}
            }
            else if(stYear == edYear && stMonth == edMonth && stDay == edDay && stTime > edTime){
            	JOptionPane.showMessageDialog(null, "Your start time (" + startHour+":"+startMin + ")  must be less than or equal to your end time (" + endHour+":"+endMin + "). Changed the start time to " +endHour+":"+endMin);
            	stTime = edTime; 
            }
          
            
            String finalStartDateTime= (ErrorCheck2.format(stYear, stMonth, stDay, stTime));
            String finalEndDateTime = (ErrorCheck2.format(edYear, edMonth, edDay, edTime));
                                    
            BufferedWriter outputStream;  
                 String content = "";
                 String sendToPrintOut = "";
                 	try{
                                    outputStream = new BufferedWriter(new FileWriter(new File("mycalendar.ics")));
                                   
                                                                      
                                    if (ocurrence.equals("None")) {
                                    	content =
                                    			PrintOut.getNonePrintOut(timeZone, finalStartDateTime, finalEndDateTime, event, location, comments, rClass, resource);
                                    }
                                    if (ocurrence.equals("Daily")) {
                                    	content = 
                                    			PrintOut.getDailyPrintOut(timeZone, finalStartDateTime, finalEndDateTime, event, location, comments, rClass, resource);
                                    }
                                    if (ocurrence.equals("Monthly")) {
                                    	content = 
                                    			PrintOut.getMonthlyPrintOut(timeZone, finalStartDateTime, finalEndDateTime, event, location, comments, rClass, resource);
                                    }
                                    if (ocurrence.equals("Yearly")) {
                                    	content =
                                    			PrintOut.getYearlyPrintOut(timeZone, finalStartDateTime, finalEndDateTime, event, location, comments, rClass, resource);
                                    }
                                    
                                   
                                    outputStream.write(content);
                                    outputStream.close();
                                    
                                }
                                catch(IOException ie){
                                   System.out.print("Unable to create file.");
                                }
                        }
                });
                btnGenerateFile.setBounds(176, 663, 117, 29);
                panel.add(btnGenerateFile);
                
                
                
                frame.setVisible(true);
                                        
        }
}